package com.java.eight.lambda;

import jdk.management.resource.ResourceId;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.*;

/**
 * @version v1.0
 * @description: lambda表达式练习
 * @author: xiaoliang.liu
 * @date: 2018/7/8 12:51
 */
public class LambdaExpress {


    /**
     * 主函数
     */
    public static void main(String[] args) {

        // 原始方法排序(根据苹果颜色排序)
        Comparator<Apple> sortByColor = new Comparator<Apple>() {

            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getColor().compareTo(o2.getColor());
            }
        };
        List<Apple> list = Collections.emptyList();
        list.sort(sortByColor);


        // 使用lambda表达式排序
        Comparator<Apple> sortByWeight = (o1, o2) -> o1.getWeight().compareTo(o2.getWeight());
        list.sort(sortByWeight);

        // 使用流（函数式）排序
        Comparator<Apple> sortByWeight2 = Comparator.comparing(Apple::getWeight);
        list.sort(sortByWeight2);


        /** 以下是合法的lambda表达式 */
        // 函数类接收  String=入参类型 Integer=返回值类型
        Function<String, Integer> flambda = s -> s.length();

        // 接收boolean类型的返回值
        Predicate<Apple> p = a -> a.getColor().equals("green");

        // 合法
        ToIntBiFunction<Integer, Integer> result = (Integer x, Integer y) -> {
            System.out.println(x);
            System.out.println(y);
            return x + y;
        };

        // 合法
        DoubleSupplier doubleSupplier = () -> 42;

        // 合法
        Runnable runnable = () -> {};

        // 合法
        ResourceId resourceId = () -> {
            return "434";
        };

    }

}
